//
//  LoopsView.m
//  薄荷家具
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoopsView.h"
#import "UIImageView+WebCache.h"
@implementation LoopsView
-(void)dealloc
{
    [_imageUrlArray release];
    [_imageView release];
    [_timer release];
    [super dealloc];
}

-(id)initWithFrame:(CGRect)frame andArray:(NSArray *)array andinterval:(NSTimeInterval)interval
{
    if (self = [super initWithFrame:frame]) {
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        self.imageUrlArray = array;
        [self show:interval];
    }
    return self;
}



-(void)show:(NSTimeInterval)interval
{
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrlArray[0]]];
    [self addSubview:self.imageView];
    
    self.userInteractionEnabled = YES;
    self.imageView.userInteractionEnabled = YES;
    
    //添加手势(轻扫)
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeGesture:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self addGestureRecognizer:leftSwipe];
    //右
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeGesture:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self addGestureRecognizer:rightSwipe];
    
    [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(leftSwipeGesture:) userInfo:nil repeats:YES];
}

#pragma mark 向左滑动浏览下一张图片
-(void)leftSwipeGesture:(UISwipeGestureRecognizer *)gesture{
    [self tranistionAnimation:YES];
}
#pragma mark 向右滑动浏览上一张图片
-(void)rightSwipeGesture:(UISwipeGestureRecognizer *)gesture{
    [self tranistionAnimation:NO];
}

#pragma 转场动画，根据滑动看下一张或上图片
-(void)tranistionAnimation:(BOOL)isNext{
    //1.转场动画
    CATransition *transition = [CATransition animation];
    transition.type = @"cube";
    //    typedef enum : NSUInteger {
    //        fade = 1,                   //淡入淡出
    //        push,                       //推挤
    //        reveal,                     //揭开
    //        moveIn,                     //覆盖
    //        cube,                       //立方体
    //        suckEffect,                 //吮吸
    //        oglFlip,                    //翻转
    //        rippleEffect,               //波纹(水滴)
    //        pageCurl,                   //翻页
    //        pageUnCurl,                 //反翻页
    //        cameraIrisHollowOpen,       //开镜头
    //        cameraIrisHollowClose,      //关镜头
    //
    //    } AnimationType;
    
    if (isNext) {
        transition.subtype = kCATransitionFromRight;
    }else {
        transition.subtype = kCATransitionFromLeft;
    }
    //动画时长
    transition.duration = 0.5f;
    
    //取下一张图片
    if (isNext) {
        _currentIndex = (_currentIndex + 1) % self.imageUrlArray.count;
    }else {
        _currentIndex = (_currentIndex + 1 + self.imageUrlArray.count) % self.imageUrlArray.count;
    }
    NSURL *imageURL = [NSURL URLWithString:self.imageUrlArray[_currentIndex]];
    [self.imageView sd_setImageWithURL:imageURL];
    
    //给转场后的新视图添加转场动画
    [self.imageView.layer addAnimation:transition forKey:nil];
    
}




-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//    [self.delegate sendCurrentIndex:self.currentIndex];
    NSLog(@"%ld",self.currentIndex);
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
